/*
 * UserInfo.java
 *
 * Created on 25 Март 2008 г., 19:45
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.jimm.protocol;

import org.jimm.app.R;
import org.jimm.comm.Util;
import org.jimm.protocol.icq.Icq;

import android.app.Activity;
import android.app.Dialog;
import android.view.ContextMenu;
import android.widget.TextView;

/**
 * @author vladimir
 */
public class UserInfo {
    private final Protocol protocol;
    private boolean avatarIsLoaded = false;
    private boolean searchResult = false;
    private Dialog dialog;

    /**
     * Creates a new instance of UserInfo
     */
    public UserInfo(Protocol prot, String uin, Activity activity) {
        protocol = prot;
        realUin = uin;
        dialog = new Dialog(activity);
        dialog.setContentView(R.layout.contact_info);
    }

    public UserInfo(Protocol p, Activity activity) {
        this(p, null, activity);
    }

    public Dialog getDialog() {
        return dialog;
    }

    void setSeachResultFlag() {
        searchResult = true;
    }


    private static final int INFO_MENU_COPY = 1040;
    private static final int INFO_MENU_COPY_ALL = 1041;
    private static final int INFO_MENU_EDIT = 1044;
    private static final int INFO_MENU_REMOVE_AVATAR = 1045;
    private static final int INFO_MENU_ADD_AVATAR = 1046;
    private static final int INFO_MENU_TAKE_AVATAR = 1047;

///////////////////////////////////////////////////////////////////////////

    public void setOptimalName() {
        Contact contact = protocol.getItemByUIN(uin);
        if (null != contact) {
            String name = contact.getName();
            if (name.equals(contact.getUserId()) || name.equals(protocol.getUniqueUserId(contact))) {
                String nick = getOptimalName();
                if (nick.length() != 0) {
                    protocol.renameContact(contact, nick);
                }
            }
        }
    }

    public synchronized void updateProfileView() {
        if ((null == dialog)) {
            return;
        }
//        dialog.setTitle(R.string.info_main);
        //((TextView)dialog.findViewById(protocol.getUserIdName()).setText(uin);
//        // #sijapp cond.if protocols_ICQ is "true" #
//        profileView.addParamImage("user_statuses", getStatusAsIcon());
//        // #sijapp cond.end #
        ((TextView) dialog.findViewById(R.id.info_nick)).setText(nick);
        ((TextView) dialog.findViewById(R.id.info_name)).setText(getName());
        ((TextView) dialog.findViewById(R.id.info_gender)).setText(getGenderAsString());
        if (0 < age) {
            ((TextView) dialog.findViewById(R.id.info_age)).setText(Integer.toString(age));
        }
        ((TextView) dialog.findViewById(R.id.info_email)).setText(email);
        //if (auth) {
        //	((TextView)profileView.findViewById("auth", JLocale.getString("yes"));
        //}
        ((TextView) dialog.findViewById(R.id.info_birth_day)).setText(birthDay);
        ((TextView) dialog.findViewById(R.id.info_cell_phone)).setText(cellPhone);
        ((TextView) dialog.findViewById(R.id.info_home_page)).setText(homePage);
        ((TextView) dialog.findViewById(R.id.info_interests)).setText(interests);
        ((TextView) dialog.findViewById(R.id.info_notes)).setText(about);

        //profileView.findViewById(R.id.info_home_info");
        ((TextView) dialog.findViewById(R.id.info_home_addr)).setText(homeAddress);
        ((TextView) dialog.findViewById(R.id.info_home_city)).setText(homeCity);
        ((TextView) dialog.findViewById(R.id.info_home_state)).setText(homeState);
        ((TextView) dialog.findViewById(R.id.info_home_phone)).setText(homePhones);
        ((TextView) dialog.findViewById(R.id.info_home_fax)).setText(homeFax);

        //profileView.findViewById("work_info");
        ((TextView) dialog.findViewById(R.id.info_work_title)).setText(workCompany);
        ((TextView) dialog.findViewById(R.id.info_work_depart)).setText(workDepartment);
        ((TextView) dialog.findViewById(R.id.info_work_position)).setText(workPosition);
        ((TextView) dialog.findViewById(R.id.info_work_addr)).setText(workAddress);
        ((TextView) dialog.findViewById(R.id.info_work_city)).setText(workCity);
        ((TextView) dialog.findViewById(R.id.info_work_state)).setText(workState);
        ((TextView) dialog.findViewById(R.id.info_work_phone)).setText(workPhone);
        ((TextView) dialog.findViewById(R.id.info_work_fax)).setText(workFax);

//        profileView.setHeader("avatar");
//        profileView.addParamImage(null, avatar);
//        

        // #sijapp cond.if (protocols_MRIM is "true") or (protocols_ICQ is "true") #
        if ((null != uin) && !avatarIsLoaded) {
            avatarIsLoaded = true;
            boolean hasAvatarItem = false;
            // #sijapp cond.if protocols_MRIM is "true"#
//            hasAvatarItem |= (protocol instanceof Mrim);
            // #sijapp cond.end #
            // #sijapp cond.if protocols_ICQ is "true"#
            hasAvatarItem |= (protocol instanceof Icq);
            // #sijapp cond.end #
            if (hasAvatarItem) {
                protocol.getAvatar(this);
            }
        }
        // #sijapp cond.end #

        if (searchResult) {
            return;
        }

//        MenuModel menu = new MenuModel();
//        menu.addItem("copy_text",     INFO_MENU_COPY);
//        menu.addItem("copy_all_text", INFO_MENU_COPY_ALL);
//        if (isEditable()) {
//            menu.addItem("edit",      INFO_MENU_EDIT);
//            // #sijapp cond.if protocols_JABBER is "true" #
//            // #sijapp cond.if modules_FILES="true"#
//            if (protocol instanceof Jabber) {
//                // #sijapp cond.if target is "MIDP2" #
//                menu.addItem("take_photo", INFO_MENU_TAKE_AVATAR);
//                // #sijapp cond.end #
//                if (jimm.modules.fs.FileSystem.isSupported()) {
//                    menu.addItem("add_from_fs", INFO_MENU_ADD_AVATAR);
//                }
//                menu.addItem("remove", INFO_MENU_REMOVE_AVATAR);
//            }
//            // #sijapp cond.end #
//            // #sijapp cond.end #
//        }
//        menu.setActionListener(this);
//        profileView.setMenu(menu, INFO_MENU_COPY);
    }

    public void setProfileViewToWait() {
        //profileView.clear();
        //profileView.addParam(protocol.getUserIdName(), uin);
        //profileView.setInfoMessage(JLocale.getString("wait"));
        //MenuModel menu = new MenuModel();
        //menu.addItem("copy_text",     INFO_MENU_COPY);
        //menu.addItem("copy_all_text", INFO_MENU_COPY_ALL);
        //menu.setActionListener(this);
        //profileView.setMenu(menu, INFO_MENU_COPY);
    }

    public boolean isEditable() {
        // #sijapp cond.if protocols_ICQ is "true" #
        if (protocol instanceof Icq) {
            return protocol.getUserId().equals(uin) && protocol.isConnected();
        }
        // #sijapp cond.end #

        // #sijapp cond.if protocols_JABBER is "true" #
        //if (protocol instanceof Jabber) {
        //   return protocol.getUserId().equals(uin) && protocol.isConnected();
        //}
        // #sijapp cond.end #
        return false;
    }

    public void select(ContextMenu menu, int cmd) {
        switch (cmd) {
            case INFO_MENU_COPY:
            case INFO_MENU_COPY_ALL:
                //profileView.copy(INFO_MENU_COPY_ALL == cmd);
                //profileView.restore();
                //break;

                //case MenuModel.BACK_COMMAND_CODE:
                //profileView.back();
                //profileView.clear();
                //break;

                // #sijapp cond.if protocols_ICQ is "true" | protocols_JABBER is "true" | protocols_OBIMP is "true" #
            case INFO_MENU_EDIT:
                //new EditInfo(protocol, this).show();
                break;
            // #sijapp cond.end #
            // #sijapp cond.if protocols_JABBER is "true" #
            // #sijapp cond.if modules_FILES="true"#
            // #sijapp cond.if target is "MIDP2" #
            case INFO_MENU_TAKE_AVATAR:
                //ViewFinder vf = new ViewFinder();
                //vf.setPhotoListener(this);
                //vf.show();
                break;

            // #sijapp cond.end #
            case INFO_MENU_REMOVE_AVATAR:
                //removeAvatar();
                //protocol.saveUserInfo(this);
                //updateProfileView();
                //profileView.restore();
                break;

            case INFO_MENU_ADD_AVATAR:
                //FileBrowser fsBrowser = new FileBrowser(false);
                //fsBrowser.setListener(this);
                //fsBrowser.activate();
                break;
            // #sijapp cond.end #
            // #sijapp cond.end #
        }
    }

    public final String realUin;

    public String uin;
    public String nick;
    public String email;
    public String homeCity;
    public String firstName;
    public String lastName;

    public String homeState;
    public String homePhones;
    public String homeFax;
    public String homeAddress;
    public String cellPhone;

    public String homePage;
    public String interests;

    public String about;

    public String workCity;
    public String workState;
    public String workPhone;
    public String workFax;
    public String workAddress;
    public String workCompany;
    public String workDepartment;
    public String workPosition;
    public String birthDay;

    public int age;
    public byte gender;
    //public String auth;
    public boolean auth; // required

    // #sijapp cond.if protocols_ICQ is "true" #
    public String status;

    @SuppressWarnings("unused")
    private int getStatusAsIcon() {
        if (null == status) {
            return 0;
        }
        byte statusIndex = StatusInfo.STATUS_NA;
        switch (Util.strToIntDef(status, 0)) {
            case 0:
                statusIndex = StatusInfo.STATUS_OFFLINE;
                break;
            case 1:
                statusIndex = StatusInfo.STATUS_ONLINE;
                break;
            case 2:
                statusIndex = StatusInfo.STATUS_INVISIBLE;
                break;
        }
        return protocol.getStatusInfo().getIcon(statusIndex);
    }

    // #sijapp cond.end #
    // Convert gender code to string
    public String getGenderAsString() {
        switch (gender) {
            case 1:
                return "female";
            case 2:
                return "male";
        }
        return "";
    }

    private String packString(String str) {
        return (null == str) ? "" : str.trim();
    }

    public String getName() {
        return packString(packString(firstName) + " " + packString(lastName));
    }

    public String getOptimalName() {
        String optimalName = packString(nick);
        if (optimalName.length() == 0) {
            optimalName = packString(getName());
        }
        if (optimalName.length() == 0) {
            optimalName = packString(firstName);
        }
        if (optimalName.length() == 0) {
            optimalName = packString(lastName);
        }
        return optimalName;
    }

}
